﻿namespace TradePublish.Service.impl
{
    using TradePublish.Common;
    using log4net;
    using TradePublish.Common;
    using System;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Text.RegularExpressions;
    using System.Threading;

    internal class SogouManager
    {
        private ILog log = null;

        public SogouManager()
        {
            this.log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        }

        private List<string> findInfoUrls(string html)
        {
            if ((html == null) || (html.Length == 0))
            {
                return null;
            }
            if (html.IndexOf("您是不是要找") > 0)
            {
                return null;
            }
            int index = html.IndexOf("class=\"rb\"");
            if (index < 0)
            {
                return null;
            }
            List<string> list = new List<string>();
            int startIndex = html.IndexOf("<a ", index);
            string item = this.trueUrl(html.Substring(startIndex, html.IndexOf(">", startIndex) - startIndex));
            if (item != null)
            {
                list.Add(item);
            }
            int num3 = html.IndexOf("class=\"rb\"", (int) (index + 10));
            if (num3 > 0)
            {
                int num4 = html.IndexOf("<a ", num3);
                string str2 = this.trueUrl(html.Substring(num4, html.IndexOf(">", num4) - num4));
                if (str2 != null)
                {
                    list.Add(str2);
                }
                int num5 = html.IndexOf("class=\"rb\"", (int) (num3 + 10));
                if (num5 > 0)
                {
                    int num6 = html.IndexOf("<a ", num5);
                    string str3 = this.trueUrl(html.Substring(num6, html.IndexOf(">", num6) - num6));
                    if (str3 != null)
                    {
                        list.Add(str3);
                    }
                }
            }
            return list;
        }

        private string getP(string htmlCode)
        {
            if (htmlCode != null)
            {
                string pattern = "name=p value=[0-9]+>";
                MatchCollection matchs = new Regex(pattern, RegexOptions.IgnoreCase).Matches(htmlCode);
                int num = 0;
                while (num <= (matchs.Count - 1))
                {
                    string str2 = matchs[num].ToString().Substring(13);
                    return str2.Substring(0, str2.Length - 1);
                }
            }
            return null;
        }

        private string getW(string htmlCode)
        {
            if (htmlCode != null)
            {
                string pattern = "name=w value=[0-9]+>";
                MatchCollection matchs = new Regex(pattern, RegexOptions.IgnoreCase).Matches(htmlCode);
                int num = 0;
                while (num <= (matchs.Count - 1))
                {
                    string str2 = matchs[num].ToString().Substring(13);
                    return str2.Substring(0, str2.Length - 1);
                }
            }
            return null;
        }

        public List<string> graspUrl(string keyword)
        {
            Exception exception;
            bool flag;
            int num = 0;
            string htmlCode = null;
            while (true)
            {
                flag = true;
                try
                {
                    GetData data = new GetData();
                    htmlCode = data.GetResponse("http://www.sogou.com/", null);
                    if (htmlCode == null)
                    {
                        throw new Exception("%%%%%%访问sogou首页，返回null");
                    }
                    break;
                }
                catch (Exception exception1)
                {
                    exception = exception1;
                    num++;
                    Thread.Sleep(0x3e8);
                    this.log.Error(string.Concat(new object[] { "=====第", num, "次访问sogou首页发生异常。", exception.Message }));
                    if (num > 2)
                    {
                        htmlCode = null;
                        break;
                    }
                }
            }
            if (htmlCode == null)
            {
                return null;
            }
            string str2 = null;
            string str3 = null;
            if (htmlCode != null)
            {
                str2 = this.getW(htmlCode);
                str3 = this.getP(htmlCode);
            }
            if ((str2 == null) || (str3 == null))
            {
                this.log.Error("=====访问sogou首页发生异常，无name=w 和 name=p。");
            }
            List<string> list = new List<string>();
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("query", keyword);
            parameters.Add("_asf", "www.sogou.com");
            parameters.Add("_ast", "1345383433");
            parameters.Add("w", str2);
            parameters.Add("p", str3);
            num = 0;
            while (true)
            {
                flag = true;
                try
                {
                    htmlCode = new GetData().GetResponse("http://www.sogou.com/web", parameters);
                    if (htmlCode == null)
                    {
                        throw new Exception("返回null");
                    }
                    break;
                }
                catch (Exception exception2)
                {
                    exception = exception2;
                    num++;
                    Thread.Sleep(0x3e8);
                    this.log.Error(string.Concat(new object[] { "=====第", num, "次访问sogou发生异常。key=", keyword, exception.Message }));
                    if (num > 2)
                    {
                        htmlCode = null;
                        break;
                    }
                }
            }
            return this.findInfoUrls(htmlCode);
        }

        private string trueUrl(string aHtml)
        {
            aHtml = aHtml.Substring(aHtml.IndexOf("href=\"") + "href=\"".Length);
            string url = aHtml.Substring(0, aHtml.IndexOf("\""));
            if ((url.IndexOf("http://www.sogou") < 0) && Utils.isTopDomain(url))
            {
                return url;
            }
            return null;
        }
    }
}

